package com.lzq.mq.broker.model;

import lombok.Data;

import java.nio.ByteBuffer;

/**
 * 消费者队列元素模型
 */
@Data
public class ConsumeQueueElementModel {

    /**
     * commitLog文件名(使用的时候转化为String)
     */
    private int commitLogFileIndex;

    /**
     * 消息的偏移量
     */
    private int msgOffset;

    /**
     * 消息长度
     */
    private int msgLength;

    public byte[] coverToBytes() {
        ByteBuffer buffer = ByteBuffer.allocate(12);
        buffer.putInt(commitLogFileIndex);
        buffer.putInt(msgOffset);
        buffer.putInt(msgLength);
        return buffer.array();
    }

    public static ConsumeQueueElementModel coverFromBytes(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        ConsumeQueueElementModel consumeQueueElementModel = new ConsumeQueueElementModel();
        consumeQueueElementModel.setCommitLogFileIndex(buffer.getInt());
        consumeQueueElementModel.setMsgOffset(buffer.getInt());
        consumeQueueElementModel.setMsgLength(buffer.getInt());
        return consumeQueueElementModel;
    }
}
